package com.xyzh05.webapp.action.android;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.beanutils.LazyDynaBean;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.Department;
import com.xyzh05.model.LoginLog;
import com.xyzh05.model.Notepad;
import com.xyzh05.model.Role;
import com.xyzh05.model.User;
import com.xyzh05.service.DictItemManager;
import com.xyzh05.service.NotepadManager;
import com.xyzh05.util.DateUtil;
import com.xyzh05.util.PageUtil;
import com.xyzh05.webapp.action.BaseAction;

public class NotepadAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = 1085039356914430936L;
	
	private NotepadManager notepadManager;
    private DictItemManager dictItemManager;
    private List<Long> userIds = new ArrayList<Long>();
    private List<Long> deptIds = new ArrayList<Long>();
    private List<Notepad> notepadList = new ArrayList<Notepad>();
    private List<Department> deptList = new ArrayList<Department>();
    private List<Role> roleList = new ArrayList<Role>();
    private Notepad notepad;
    private Integer currentPage;
    private Integer pageSize;
    private Long id;
    private Short period;
    private Short type;
    private String content;
    private Integer actionType;
	private String location;
	private String longitude;
	private String latitude;
    private Map<String, Object> jsonMap = new HashMap<String, Object>();

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }

	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

	public List<Long> getDeptIds() {
		return deptIds;
	}

	public void setDeptIds(List<Long> deptIds) {
		this.deptIds = deptIds;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	
    public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public void setId(Long id) {
        this.id = id;
    }

    public Notepad getNotepad() {
        return notepad;
    }

    public void setNotepad(Notepad notepad) {
        this.notepad = notepad;
    }

	public Integer getActionType() {
		return actionType;
	}

	public void setActionType(Integer actionType) {
		this.actionType = actionType;
	}

	public NotepadManager getNotepadManager() {
		return notepadManager;
	}

    public void setNotepadManager(NotepadManager notepadManager) {
        this.notepadManager = notepadManager;
    }
    
	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Long getId() {
		return id;
	}

	public Short getPeriod() {
		return period;
	}

	public void setPeriod(Short period) {
		this.period = period;
	}

	public Short getType() {
		return type;
	}

	public void setType(Short type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<Notepad> getNotepadList() {
		return notepadList;
	}

	public void setNotepadList(List<Notepad> notepadList) {
		this.notepadList = notepadList;
	}

	public DictItemManager getDictItemManager() {
		return dictItemManager;
	}

	public void setDictItemManager(DictItemManager dictItemManager) {
		this.dictItemManager = dictItemManager;
	}

	public List<Long> getUserIds() {
		return userIds;
	}

	public void setUserIds(List<Long> userIds) {
		this.userIds = userIds;
	}

    /**
     * 保存记事、签到信息
     * @return
     * @throws Exception
     */
    public String saveNotepad() {
    	notepad = new Notepad();
    	notepad.setUser(userManager.get(id));
    	notepad.setSignInTime(new Date());
		notepad.setMode(LoginLog.MODE_MOBILE);
		notepad.setType(type);
		notepad.setContent(content);
		notepad.setLocation(location);
		notepad.setLongitude(longitude);
		notepad.setLatitude(latitude);
		
    	if (actionType == 1) {
    		notepad.setPeriod(period);
			notepad.setDictItem(dictItemManager.get(Notepad.SIGN.longValue()));
		} else {			
			notepad.setDictItem(dictItemManager.get(Notepad.NOTE.longValue()));
		}
    	
		try {
			notepad = notepadManager.save(notepad);
			
	        jsonMap.put("status", "1");
	        jsonMap.put("data", "");
	        jsonMap.put("info", "");
	        jsonMap.put("errorCode", "");
		} catch (Exception e) {
            jsonMap.put("status", "0");
            jsonMap.put("data", "");
            jsonMap.put("info", "");
            jsonMap.put("errorCode", "1000");
		}
    	return SUCCESS;
    }
    
    /**
     * 加载签到、记事信息
     * @return
     */
    public String searchNotepad(){
    	User user = userManager.get(id);
    	userIds.add(user.getId());
    	deptIds.add(user.getUserDepartment().getId());
    	if(user.isCompanyRole()){
    		getDeptUsers();
    	}
    	try {
    		if(actionType == 2){
        		notepadList = notepadManager.searchNotepad(userIds, Notepad.SIGN, currentPage==null&&pageSize==null?0:PageUtil.getStartNum(currentPage, pageSize), pageSize==null?10:pageSize);
        	}else if(actionType == 3){
        		notepadList = notepadManager.searchNotepad(userIds, Notepad.NOTE, currentPage==null&&pageSize==null?0:PageUtil.getStartNum(currentPage, pageSize), pageSize==null?10:pageSize);
        	}else if(actionType == 4){
        		getDeptUsers();
        		notepadList = notepadManager.searchNotepad(userIds, null, currentPage==null&&pageSize==null?0:PageUtil.getStartNum(currentPage, pageSize), pageSize==null?10:pageSize);
        	}
    		
    		/**
    		// 先过滤对set集合的拆解
    		JsonConfig config = new JsonConfig();
    		config.setJsonPropertyFilter(new PropertyFilter() {
    			public boolean apply(Object arg0, String arg1, Object arg2) {
			 		if (arg1.equals("comments")) {
			 			return true;
			 		} else {
			 			return false;
			 		}
    			}
    		});
    		// 将数据转换成Json数据
    		JSONArray jsonObject = JSONArray.fromObject(notepadList, config);
    		System.out.println(jsonObject.toString());
    		*/
    		
    		List<LazyDynaBean> ldbList = new ArrayList<LazyDynaBean>();
    		for(Notepad np : notepadList){
    			LazyDynaBean ldb = new LazyDynaBean();
    			ldb.set("id", np.getId());
    			ldb.set("dictItem", np.getDictItem().getName());
    			ldb.set("user", np.getUser().getDisplayName());
    			ldb.set("signInTime", DateUtil.getDateTime("yyyy-MM-dd", np.getSignInTime()));
    			ldb.set("content", np.getContent());
    			ldb.set("mode", np.getMode());
    			ldb.set("period", np.getPeriod());
    			ldb.set("type", np.getType());
    			ldb.set("location", np.getLocation() == null ? "" : np.getLocation());
    			ldb.set("longitude", np.getLongitude() == null ? "" : np.getLongitude());
    			ldb.set("latitude", np.getLatitude() == null ? "" : np.getLatitude());
    			ldbList.add(ldb);
    		}
    		
            jsonMap.put("status", "1");
            jsonMap.put("data", JSONArray.fromObject(ldbList));
            jsonMap.put("info", "");
            jsonMap.put("errorCode", "");
		} catch (Exception e) {
            jsonMap.put("status", "0");
            jsonMap.put("data", "");
            jsonMap.put("info", "");
            jsonMap.put("errorCode", "1000");
		}
    	return SUCCESS;
    }

    private void getDeptUsers(){
		deptList = departmentManager.getDepartmentById(id);
    	for(Department dept : deptList){
    		deptIds.add(dept.getId());
    	}
		roleList = roleManager.getUserBindList(deptIds, null, Role.DEFAULT_ROLE_EMPLOYEE.shortValue(), null);
		if(roleList != null && roleList.size() > 0){
			userIds.clear();
			for(Role r : roleList){
				userIds.add(r.getUser().getId());
			}
		}
    } 

}